package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiveParam;
import com.iwhalecloud.uncc.model.InsCollectServerResRate;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/27 11:41
 * @description
 */
@Repository("insCollectServerResRateArchiverDao")
public class InsCollectServerResRateArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_COLLECT_SERVER_RES_RATE WHERE SERVER_ID = ? AND CREATE_DATE BETWEEN ? AND ? AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        List<InsCollectServerResRate> list = jdbcTemplate.query(sql.toString(), new Object[]{o,aP.getStartDate(),aP.getEndDate()} , new BeanPropertyRowMapper<>(InsCollectServerResRate.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `SERVER_ID`, `SERVICE_ID`, `CPU_TOTAL`, `MEMORY_TOTAL`, `DISK_TOTAL`, `IO_WAIT`, `IO_STAT`, `CREATE_DATE`, `PARTITION_CODE`, `STATE`");
        sql.append(" FROM INS_COLLECT_SERVER_RES_RATE WHERE SERVER_ID in (:i)");
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        int limit = (list.size())*(aP.getEachInsLines());
        sql.append(" AND CREATE_DATE BETWEEN \"" + aP.getStartDate() + "\" AND \"" + aP.getEndDate()+"\"");
        sql.append(" AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        sql.append(" LIMIT " + limit);
        List<InsCollectServerResRate> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsCollectServerResRate insCollectServerResRate = new InsCollectServerResRate();
                insCollectServerResRate.setId(resultSet.getLong("ID"));
                insCollectServerResRate.setServiceId(resultSet.getString("SERVICE_ID"));
                insCollectServerResRate.setServerId(resultSet.getString("SERVER_ID"));
                insCollectServerResRate.setCpuTotal(resultSet.getString("CPU_TOTAL"));
                insCollectServerResRate.setMemoryTotal(resultSet.getString("MEMORY_TOTAL"));
                insCollectServerResRate.setDiskTotal(resultSet.getString("DISK_TOTAL"));
                insCollectServerResRate.setIoWait(resultSet.getString("IO_WAIT"));
                insCollectServerResRate.setIoStat(resultSet.getString("IO_STAT"));
                insCollectServerResRate.setCreateDate(resultSet.getTimestamp("CREATE_DATE"));
                insCollectServerResRate.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                insCollectServerResRate.setState(resultSet.getString("STATE"));
                return insCollectServerResRate;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_collect_server_res_rate`");
        sql.append(" (`ID`, `SERVER_ID`, `SERVICE_ID`, `CPU_TOTAL`, `MEMORY_TOTAL`, `DISK_TOTAL`, `IO_WAIT`, `IO_STAT`, `CREATE_DATE`, `PARTITION_CODE`, `STATE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id,:serverId,:serviceId,:cpuTotal,:memoryTotal,:diskTotal,:ioWait,:ioStat,:createDate,:partitionCode,:state,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_COLLECT_SERVER_RES_RATE SET ARCHIVER_FLAG = " + flag + " WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_COLLECT_SERVER_RES_RATE WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
